<?php
namespace App\Http\Controllers\Open;
use App\Http\Controllers\Common\CommonController;
use App\Models\Common\CollArea;
use App\Models\Common\CollOption;
use App\Models\Common\CollPosition;
use App\Models\Personal\ResumeEducationExp;
use App\Models\Personal\ResumeWorkExp;
use App\Repositories\Eloquent\YiwangAccessToken;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use App\Models\Api\Resume;

class ResumeController extends CommonController{

    private $webUrl = 'http://demo.15hr.com';//该站网址
    public function __construct(Request $request)
    {
        $yiwangAccessToken = new YiwangAccessToken();
        $yiwangAccessToken->checkAccessToken($request->access_token);
    }

    public function index(){
        $resume = DB::table('resume')
               ->select(
                'id','uid','name','sex','birthday','education','homeaddress','census',
                'work_year','intentionjobs','jobsort1','jobsort2','jobsort3','jobsort4','jobsort5','jobarea1','jobarea2','jobarea3',
                'jobarea4','jobarea5','updatetime','avatar','resume_status'
               )
               ->orderBy('updatetime','desc')
               ->paginate(20);
        $data = array();
        foreach($resume as $k=>$v){
            switch($v->sex){
                case 0:
                    $v->sex='不限';
                    break;
                case 1:
                    $v->sex='男';
                    break;
                case 2:
                    $v->sex='女';
                    break;
            }

            //岗位类别
            $v->jobsort1 = CollPosition::getPosName($v->jobsort1);
            $v->jobsort2 = CollPosition::getPosName($v->jobsort2);
            $v->jobsort3 = CollPosition::getPosName($v->jobsort3);
            $v->jobsort4 = CollPosition::getPosName($v->jobsort4);
            $v->jobsort5 = CollPosition::getPosName($v->jobsort5);

            $v->category =  $v->jobsort1?$v->jobsort1.';':'';
            $v->category .=  $v->jobsort2?$v->jobsort2.';':'';
            $v->category .=  $v->jobsort3?$v->jobsort3.';':'';
            $v->category .=  $v->jobsort4?$v->jobsort4.';':'';
            $v->category .=  $v->jobsort5?$v->jobsort5.';':'';

            //意向地区
            $v->jobarea1 = CollArea::getAreaName($v->jobarea1);
            $v->jobarea2 = CollArea::getAreaName($v->jobarea2);
            $v->jobarea3 = CollArea::getAreaName($v->jobarea3);
            $v->jobarea4 = CollArea::getAreaName($v->jobarea4);
            $v->jobarea5 = CollArea::getAreaName($v->jobarea5);

            $v->intentionarea =  $v->jobarea1?$v->jobarea1.';':'';
            $v->intentionarea .=  $v->jobarea2?$v->jobarea2.';':'';
            $v->intentionarea .=  $v->jobarea3?$v->jobarea3.';':'';
            $v->intentionarea .=  $v->jobarea4?$v->jobarea4.';':'';
            $v->intentionarea .=  $v->jobarea5?$v->jobarea5.';':'';
            $resumeEducationExp = ResumeEducationExp::select('speciality')->where('resume_id',$v->id)->orderBy('education','desc')->first();

            $resumeWorkExp = ResumeWorkExp::select('company','industry','post')->where('resume_id',$v->id)->orderBy('starttime','desc')->get();

            $v->exp_company = ''; $v->exp_industry = ''; $v->exp_post = '';
            foreach($resumeWorkExp as $rwk=>$rwv){
                $v->exp_company.=$rwv->company?$rwv->company.';':'';
                $exp_industry=CollOption::getOptionName($rwv->industry,'industry');
                $v->exp_industry.=$exp_industry?$exp_industry.';':'';
                $v->exp_post.=$rwv->post?$rwv->post.';':'';
            }

            $data[] = array(
                'resumeid'=>$v->id,//简历id
                'name'=>$v->name,//姓名
                'sex'=>$v->sex,//性别
                'birthday'=>$v->birthday,//生日
                'intentionjobs'=>$v->intentionjobs,//意向岗位
                'category'=>$v->category,//岗位类别,多个用分号分开
                'education'=>CollOption::getOptionName($v->education,'education'),//学历
                'specialty'=>empty($resumeEducationExp->speciality)? '' : $resumeEducationExp->speciality,//专业
                'intentionarea'=>$v->intentionarea,//意向地区，多个秀分号分开
               
                'livingarea'=>CollArea::getAreaName($v->homeaddress),//现在地区
                'household'=>CollArea::getAreaName($v->census),//户籍
                'exp_company'=>$v->exp_company,//工作过的公司
                'exp_industry'=>$v->exp_industry,//工作过的行业
                'exp_post'=>$v->exp_post,//工作过的岗位
                'url'=>url('resume_'.idEncryption($v->id).'.html'),//简历展示地址
                'updatetime'=>$v->updatetime//用户真实更新时间,datatime类型
            );
        }  
        return response()->json($data);
    }

    /**
     * 简历情况
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(Request $request){
        $data = Resume::where(['id'=>$request['id']])
            ->whereBetween('resume_status', [0,3])//没有被删除的
            ->first();

        if (!is_null($data)) {
            //格式化
            $data->append(['work_year_name', 'homeaddress_name', 'expectedsalary_name',
                'jobsort_name', 'jobarea_name', 'education_name', 'census_name', 'age'])
                ->makeHidden(['work_year', 'education']);
            foreach ($data->resume_workexp as $item) {
                $item->append(['industry_name', 'comkind_name', 'scale_name']);
            }

            if (empty($request->contact)) {//不存在不显示联系方式
                $data->makeHidden(['mobile', 'email', 'qq']);//隐藏联系方式
            }
            //头像
            if(empty($data->avatar)){
               $avatar = ''; 
            }else{
               $avatar = $this->webUrl.'/upload/'.$data->avatar;  
            }

            switch($data->sex){
                case 0:
                    $data->sex='未知';
                    break;
                case 1:
                    $data->sex='男';
                    break;
                case 2:
                    $data->sex='女';
                    break;
            }

            switch($data->marital){
                case 0:
                    $data->marital='未填写';
                    break;
                case 1:
                    $data->marital='未婚';
                    break;
                case 2:
                    $data->marital='已婚';
                    break;
                case 3:
                    $data->marital='离异';
                    break;
            }
            //求职状态
            switch($data->job_status){
                case 1:
                    $data->job_status='不在职，正在找工作';
                    break;
                case 2:
                    $data->job_status='在职，打算近期换工作';
                    break;
            }

            $jsonData = array();
            $jsonData['basic'] = array(
                "name" => $data->name,
                "sex" => $data->sex,
                "birthday" => $data->birthday,
                "homeaddress" => $data->homeaddress,
                "expectedsalary" =>$data->expectedsalary,
                "isexpectedsalary" => $data->isexpectedsalary,
                "intentionjobs" => $data->intentionjobs,
                "introduction" => $data->introduction,
                "avatar" => $avatar,
                "address" =>$data->address,
                "job_status" => $data->job_status,
                "resume_status" => $data->resume_status,
                "bkresume_status" => $data->bkresume_status,
                "census" =>$data->census,
                "marital" => $data->marital,
                "height" => $data->height,
                "birthday_name" => $data->birthday_name,
                "work_year_name" => $data->work_year_name,
                "homeaddress_name" => $data->homeaddress_name,
                "expectedsalary_name" =>$data->expectedsalary_name,
                "jobsort_name" => $data->jobsort_name,
                "jobarea_name" => $data->jobarea_name,
                "education_name" => $data->education_name,
                "census_name" => $data->census_name,
                "age" => $data->age,
                "update_time" => $data->updatetime->format('Y-m-d H:i:s')
            );

            //求职意向
            $jsonData['intention_jobs'] = array(
                    "intention_industry"=>$data->intentionjobs, //意向行业
                    "intention_post"=>$data->jobsort_name, //意向岗位
                    "intention_area"=>$data->jobarea_name, //意向地区
                    "salary_expectations"=>$data->expectedsalary_name.'元/月', //期望月薪
                    "status"=>$data->job_status //求职状态
            );

            //自我评价
            if(!empty($data->introduction)){
                $jsonData['introduction'] = array('content'=>$data->introduction);
            }
            $jsonData['resume_workexp'] = $data->resume_workexp->toArray();

            //教育类型
            foreach($data->resume_eduexp as $k=>$v){
                switch($v->edu_type){
                    case 1:
                        $data->resume_eduexp[$k]['edu_type']='学历教育';
                        break;
                    case 2:
                        $data->resume_eduexp[$k]['edu_type'] ='技能培训';
                        break;
                }
            }
            $jsonData['resume_eduexp'] = $data->resume_eduexp->toArray();
            $jsonData['resume_proexp'] = $data->resume_proexp->toArray();

            //技能专长--熟练程度
            foreach($data->resume_skill as $k=>$v){
                switch($v->degree){
                    case 1:
                        $data->resume_skill[$k]['degree']='入门';
                        break;
                    case 2:
                        $data->resume_skill[$k]['degree'] ='熟练';
                        break;
                    case 3:
                        $data->resume_skill[$k]['degree'] ='精通';
                        break;
                }
            }
            $jsonData['resume_skill'] = $data->resume_skill->toArray();
            $jsonData['resume_cer']   = $data->resume_cer->toArray();
            $jsonData['resume_lang']  = $data->resume_lang->toArray();
            $jsonData['resume_other'] = $data->resume_other->toArray();

            //我的作品
            foreach($data->resume_works as $k=>$v){
                $data->resume_works[$k]['title'] = $v->works_name;
                $resumeWorksImg = DB::table('resume_works_img')->select('imgurl')->where('works_id', $v->id)->get();
                $con = array();
                foreach($resumeWorksImg as $k1=>$v1){
                    if(empty($v1->imgurl)){
                        $dataImgUrl = '';
                    }else{
                        $dataImgUrl = $this->webUrl.'/upload/personalworks/'.$v1->imgurl;
                    }
                    $con[] = $dataImgUrl;
                }
                $data->resume_works[$k]['src'] = $con;
            }
            $jsonData['my_works'] = $data->resume_works->toArray();

            if($request->contact==1){
                $jsonData['contact'] = array(
                    "mobile" => $data->mobile,
                    "email" => $data->email,
                    "qq" => $data->qq
                );
            }
            $jsonData = array_filter($jsonData);
            return response()->json($jsonData);
        }
    }

}

